package com.wisezone.grademanager;

import java.util.ArrayList;
import java.util.List;

/**
 * 班级管理模块
 */
public class GradeManager {
	public static List<Grade> gradeAllLists = new ArrayList<Grade>();
	
	public GradeManager() {
		Grade grade1 = new Grade("java","2001");
		Grade grade2 = new Grade("ios","2002");
		
		gradeAllLists.add(grade1);
		gradeAllLists.add(grade2);
	}
	
	public void startGradeManager() {
		System.out.println("========班级管理主界面=============");
		boolean isBack = true;
		while (isBack) {
			System.out.println("1 创建班级, 2 显示班级, 3 查看班级学生, 4返回班级系统主界面");
			int common = Utils.inputInt();
			switch (common) {
			case 1:
				addGrade();
				break;
			case 2:
				showGrade();
				break;
			case 3:
				showStudentsFromGrade();
				break;
			case 4:
				isBack = false;
				break;
			}
		}
	}

	/**
	 * 创建班级
	 */
	public void addGrade() {
		System.out.println("创建班级");
		System.out.println("请输入班级名称");
		String name = Utils.inputString();
		System.out.println("请输入班级编号");
		String number=Utils.inputString();
		Grade grade01 = new Grade(name,number);
		gradeAllLists.add(grade01);
		System.out.println("添加班级成功");
	}

	/**
	 * 显示班级
	 */
	public void showGrade() {
		System.out.println("显示班级");
		int size=gradeAllLists.size();
		if(size==0) {
			System.out.println("没有班级");
			return;
		}
		for (Grade grade : gradeAllLists) {
			System.out.println(grade);
		}
		
	}

	/**
	 * 显示班级下所有学生
	 */
	public void showStudentsFromGrade() {
		System.out.println("请输入班级名称: ");
		String gradeName = Utils.inputString();
		//1. 根据班级名称找到班级对象
		Grade newGrade = findGradeByName(gradeName);
		
		//2. 班级下所有学生集合
		List<Student> studentLists = newGrade.getStudentLists();
		System.out.println("班级 \t学生 \t年龄\t性别");
		for (Student student : studentLists) {
			System.out.println(newGrade.getName()+"\t"+student.getName()+"\t"+student.getAge()+"\t"+student.getSex());
		}		
	}
	
	/**
	 * 根据班级名查找班级
	 * @return
	 */
	public Grade findGradeByName(String gradeName) {
		Grade newGrade = null;
		for (Grade grade : GradeManager.gradeAllLists) {
			if(gradeName.equals(grade.getName())) {
				newGrade = grade;
				break;
			}
		}
		return newGrade;
	}
}
